<?php
class csv_parser {
	
	/**
	 * This method will take a file path as the input and will return
	 * the content of it as a 2D array representing a cell array.
	 * @param String $file_path Location of the file to be opened.
	 */
	public static function csv_to_array($file_path) {
		//open a file to read
		$handle = @fopen($save_file, 'r');
		
		if ($handle) {
			$file_content = array();
			
			while ( !feof($handle) ) {
				//Reads an entire row in to a string
				$row = fread($handle);
				//Seperate cols in a row using a split functionality
				$row_cells = explode(',', $row);
				//Appending the cells to content array
				$file_content[] = $row_cells;
				
			}
			
			return $file_content;
			
		}
		
		return false;
	}	

	public static function array_to_csv($csv_array, $file_path) {
		
		//open a file to write
		$handle = @fopen($file_path, 'w');
		
		if ($handle) {
			
			foreach ($csv_array as $row) {
				$str = "";
				
				foreach ($row as $val) {
					$str = $str . $val . ',';	
				}
				
				//Remove the extra comma at the end of the file
				$str = substr($str, 0, strlen($str) - 1);

				//write to file
				fputs($handle, $str);
			}
			
			//Closing the file pointer that was opened
			fclose($handle);
			
			//Say success
			return true;
			
		}
		
		//Represents failure to write data to file
		return false;
		
	} 
	
	
}


?>